import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { FileInfo, FileForm } from "./types";
import { useUserStoreHook } from "@/store/modules/user";

const userStore = useUserStoreHook();
/**
 * 上传文件
 *
 * @param file
 */
export function uploadFileApi(
  file: File,
  form: FileForm = {}
): AxiosPromise<FileInfo> {
  const formData = new FormData();
  formData.append("file", file);
  formData.append("form", form.form);
  formData.append("module", form.module);
  formData.append("part", form.part);
  formData.append("createrId", userStore.user.sysUser?.id);

  return request({
    url: "file-server/file/uploadBatch",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}

/**
 * 删除文件
 *
 * @param filePath 文件完整路径
 */
export function deleteFileApi(id?: string) {
  return request({
    url: `file-server/file/deleteFile?id=${id}`,
    method: "post",
  });
}

/**
 * 获取文件列表
 * @param params  module,form,part
 * @returns 文件列表
 */
export function listFile(params: FileForm): AxiosPromise<FileInfo> {
  return request({
    url: `file-server/file/getFiles`,
    method: "get",
    params: params,
  });
}
